const crypto = require('crypto');

// createHash
let hash = crypto.createHash('md5');
hash.update('hello');
hash.update('world');
hash.update('helloworld');
let hashCode = hash.digest('base64');
console.log(`hashCode: ${hashCode}`);

let hmac = crypto.createHmac('md5', 'custom_key');
hmac.update('hello');
hmac.update('world');
// hmac.update('helloworld');
let hmacCode = hmac.digest('base64');
console.log(`hmacCode: ${hmacCode}`);

// base64编码
let str = 'Hello world';
let buf = Buffer.from(str); //第二个参数默认为 utf8
let encodedBuf = buf.toString('base64');
console.log(`encodedBuf: ${encodedBuf}`);

// base64解码（指明文本格式）
buf = Buffer.from(encodedBuf, 'base64');
encodedBuf = buf.toString('utf-8');
console.log(`encodedBuf: ${encodedBuf}`);